﻿using Engine.UI;
using Message;
using UnityEngine;
using UnityEngine.UI;

/// <summary>
/// 游戏主界面
/// </summary>
public class MainView : ViewBase
{
    private Button getPlayerListBtn;
    private GameObject playerBtnPrefab;
    private Joystick joystick;

    public override string GetPath()
    {
        return "game.ui.mainview";
    }

    public override void OnLoaded(Transform prefab)
    {
        base.OnLoaded(prefab);
        getPlayerListBtn = prefab.Find("GetPlayerListBtn").GetComponent<Button>();
        joystick = prefab.Find("Joystick").gameObject.AddComponent<Joystick>();
        getPlayerListBtn.onClick.AddListener(OnGetPlayerList);
        ResManager.Instance.Load("game.ui.player", "player", delegate (GameObject player)
        {
            player.name = GlobeData.uid.ToString();
            player.transform.Find("Text").GetComponent<TextMesh>().text = GlobeData.uname;
            PlayerMoveControl pc = player.AddComponent<PlayerMoveControl>();
            pc.joystick = joystick;
        });

    }

    void OnGetPlayerList()
    {
        //注册登录返回结果回调
        MsgManager.Instance.AddListener(typeof(S2CGetPlayerList), OnGetPlayerListBack);

        //发送
        C2SGetPlayerList protocol = new C2SGetPlayerList();
        protocol.type = 0;
        NetManager.Instance.Send(protocol);
    }

    void OnGetPlayerListBack(object proto)
    {
        S2CGetPlayerList stcgl = proto as S2CGetPlayerList;
        if (stcgl.players.Count > 0)
        {
            for (int i = 0; i < stcgl.players.Count; i++)
            {
                Log.Debug("uid:" + stcgl.players[i].uid + " uname:" + stcgl.players[i].uname);
            }
        }
        MsgManager.Instance.RemoveListener(typeof(S2CGetPlayerList), OnGetPlayerListBack);
    }
}